//
//  Router.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 12/26/16.
//  Copyright © 2016 com.mask. All rights reserved.
//

import Foundation
import Alamofire

enum Router: URLRequestConvertible {
    
    case login(username: String, password: String)
    case auth(username: String)
    case projectList(type: Int, page: Int)
    case searchProjectList(keyword: String, page: Int)
    case subProjectList(id: String)
    case subProjectProcess(id: String)
    case applicationsInfo(username: String)
    case subProjectUploadAllPicture(pictures: [Data], projectId: Int, subProjectId: Int)
    case reformUploadPicture(processId: Int, content: String, subProjectId: Int, projectId: Int, id: Int, endTime: Int, imageId: Int, picName: String)
    case historyUploadList(processId: Int)
    
    var baseURLString: String {
        switch self {
        case .login, .applicationsInfo:
            return "http://sso.liblue.com/mini4Aweb/servlet/"
        case .auth, .projectList, .searchProjectList, .subProjectList, .subProjectProcess, .subProjectUploadAllPicture, .reformUploadPicture, .historyUploadList:
            return "http://211.149.223.11:8080/process/servlet/"
        }
    }
    
    var method: HTTPMethod {
        switch self {
        case .login, .auth, .subProjectUploadAllPicture, .reformUploadPicture, .historyUploadList:
            return .post
        case .projectList, .searchProjectList, .subProjectList, .subProjectProcess, .applicationsInfo:
            return .get
        }
    }
    
    var path: String {
        switch self {
        case .login:
            return "loginAction"
        case .auth:
            return "AppLoginServlet"
        case .projectList:
            return "AppProListServlet"
        case .searchProjectList:
            return "AppSearchListServlet"
        case .subProjectList:
            return "AppChProListServlet"
        case .subProjectProcess:
            return "AppStepsListServlet"
        case .applicationsInfo:
            return "mainAction"
        case .subProjectUploadAllPicture:
            return "AppAddPicsServlet"
        case .reformUploadPicture:
            return "AppAddPicOneServlet"
        case .historyUploadList:
            return "AppPicListServlet"
        }
    }

    // MARK: - URLRequestConvertible
    
    func asURLRequest() throws -> URLRequest {
        let url = try baseURLString.asURL()
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))
        urlRequest.httpMethod = method.rawValue
        
        switch self {
        case let .login(username, password):
            let parameters = ["loginname": username, "loginpwd": password, "op": "login", "idfycode": "app"]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .auth(username):
            let parameters = ["username": username]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .projectList(type, page):
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["type": String(type), "cur_page": String(page)]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .searchProjectList(keyword, page):
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["search_con": String(keyword), "cur_page": String(page)]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .subProjectList(id):
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["id": id]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .subProjectProcess(id):
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["id": id]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .applicationsInfo(username):
            let parameters = ["loginname": username, "op": "appRes"]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .subProjectUploadAllPicture(pictures, projectId, subProjectId):
            urlRequest.setValue("multipart/form-data; boundary=hello", forHTTPHeaderField: "Content-Type")
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["file": pictures, "uid": User.shared.id!, "pid2": subProjectId, "pid1": projectId] as [String : Any]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .reformUploadPicture(processId, content, subProjectId, projectId, id, endTime, imageId, picName):
            let parameters = ["id": processId, "Content": content, "Pid2": subProjectId, "Pid1": projectId, "uid": id, "album": endTime, "img_id": imageId, "pic": picName] as [String : Any]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        case let .historyUploadList(processId):
            urlRequest.setValue(User.shared.auth_key, forHTTPHeaderField: "key")
            let parameters = ["id": processId]
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        }
        
        return urlRequest
    }
}
